<template>
  <el-dialog
    v-model="visible"
    :modal="false"
    :title="needReturn ? '选择发货地址' : '上传检测结果'"
    :close-on-click-modal="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataRule"
      label-width="80px"
      @keyup.enter="onSubmit()"
    >
      <el-form-item label="快递公司" v-if="needReturn">
        <el-select v-model="dataForm.dvyId" placeholder="请选择">
          <el-option
            v-for="item in dataForm.dvyNames"
            :key="item.dvyId"
            :label="item.dvyName"
            :value="item.dvyId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="快递单号" prop="dvyFlowId" v-if="needReturn">
        <el-input
          v-model="dataForm.dvyFlowId"
          controls-position="right"
          :min="0"
          label="快递单号"
        />
      </el-form-item>
      <el-form-item label="检测结果" prop="detectionResult">
        <file-upload v-model="dataForm.detectionResult" />
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, reactive, computed } from "vue";
import { ElMessage } from "element-plus";
import FileUpload from "@/components/file-upload/index.vue";
const emit = defineEmits(["refreshDataList"]);

// eslint-disable-next-line no-unused-vars
const validDvyFlowId = (rule, value, callback) => {
  if (!value.trim()) {
    callback(new Error("不能为空"));
  } else {
    callback();
  }
};
const dataRule = computed(() => {
  const rules = {
    detectionResult: [
      { required: true, message: "请上传检测结果", trigger: "blur" },
    ],
  };

  if (needReturn.value) {
    rules.dvyFlowId = [
      { required: true, message: "不能为空", trigger: "blur" },
      { validator: validDvyFlowId, trigger: "blur" },
    ];
  }

  return rules;
});

const visible = ref(false);
const needReturn = ref(false);
const dataForm = reactive({
  dvyId: "",
  dvyFlowId: 0,
  dvyNames: [],
  orderNumber: 0,
  detectionResult: "",
});

const init = (
  orderNumber,
  dvyId,
  dvyFlowId,
  detectionResult,
  needReturnValue = true
) => {
  visible.value = true;
  needReturn.value = needReturnValue;
  dataForm.orderNumber = orderNumber || "";
  dataForm.dvyId = dvyId || "";
  dataForm.dvyFlowId = dvyFlowId || "";
  dataForm.detectionResult = detectionResult || "";

  // 如果不需要快递信息，清空快递相关字段
  if (!needReturn.value) {
    dataForm.dvyId = "";
    dataForm.dvyFlowId = 0;
  }

  http({
    url: http.adornUrl("/admin/delivery/list"),
    method: "get",
    params: http.adornParams(),
  }).then(({ data }) => {
    dataForm.dvyNames = data;
  });
};
defineExpose({ init });

const dataFormRef = ref(null);
/**
 * 表单提交
 */
const onSubmit = () => {
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      http({
        url: http.adornUrl("/order/order/delivery"),
        method: "put",
        data: http.adornData({
          orderNumber: dataForm.orderNumber,
          dvyId: dataForm.dvyId,
          dvyFlowId: dataForm.dvyFlowId,
          detectionResult: dataForm.detectionResult,
        }),
      }).then(() => {
        ElMessage({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            visible.value = false;
            emit("refreshDataList");
          },
        });
      });
    }
  });
};
</script>
